package com.yingpai.fitness.dvp_2_0.main.activity.fitness;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.store.PersonalDetailBean;
import com.yingpai.fitness.dvp_2_0.main.widget.GlideImageLoader;
import com.yingpai.fitness.dvp_2_0.common.view.XGridView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.zzhoujay.richtext.RichText;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * Created by ASUS on 2018/8/14.
 */

public class PersonalDetailActivity extends CommonActivity {

    @BindView(R.id.hot_child_banner)
    Banner hotChildBanner;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.photo)
    ImageView photo;
    @BindView(R.id.item_name)
    TextView itemName;
    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.buy)
    TextView buy;
    @BindView(R.id.richtv)
    TextView richtv;
    @BindView(R.id.photo_gridview)
    XGridView photoGridview;
    @BindView(R.id.videotv)
    TextView videotv;
    @BindView(R.id.video)
    JZVideoPlayerStandard video;
    @BindView(R.id.abstractTv)
    TextView abstractTv;
    private int storeId;
    private int courseId;
    private PersonalDetailBean mPersonalDetailBean;
    private SensorManager sensorManager;
    private JZVideoPlayer.JZAutoFullscreenListener sensorEventListener;
    private QuickAdapter<String> quickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_personal_detail);
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onStop() {
        super.onStop();
        //停止
        hotChildBanner.stopAutoPlay();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //自动播
        hotChildBanner.startAutoPlay();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void initView() {
        setTitle("私教详情");

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            storeId = bundle.getInt("storeId", -1);
            courseId = bundle.getInt("courseId", -1);
        }

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JZVideoPlayer.JZAutoFullscreenListener();

        hotChildBanner.setIndicatorGravity(BannerConfig.CENTER);

        abstractTv.setText("教练简介:");
        buy.setVisibility(View.VISIBLE);
        videotv.setText("教练视频");

        quickAdapter = new QuickAdapter<String>(this, R.layout.item_course_photo) {
            @Override
            protected void convert(BaseAdapterHelper helper, String item) {
                ImageView photo = helper.getView(R.id.photo);
                Glide.with(context)
                        .load(item)
                        .centerCrop()
                        .placeholder(R.mipmap.default_big_bg)
                        .into(photo);
            }
        };
        photoGridview.setAdapter(quickAdapter);
        photoGridview.setFocusable(false);
    }

    @Override
    protected void initData() {
        showDialog(this,null);
        HttpParams params = new HttpParams();
        params.put("storeId", storeId);
        params.put("curriculumId", courseId);
        params.put("type", Constant.NUM_2);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.GET_CURRICULUM_DETAIL, params, Url.GET_CURRICULUM_DETAIL, new Callback<PersonalDetailBean>() {
            @Override
            public void onSuccess(PersonalDetailBean personalDetailBean) {
                if (Constant.REQUEST_SUCCESS.equals(personalDetailBean.getResult())) {
                    mPersonalDetailBean = personalDetailBean;

                    List bannerList = Arrays.asList(personalDetailBean.getMap().getStore().getPhotoUrls().split(","));
                    hotChildBanner.setImages(bannerList)
                            .setImageLoader(new GlideImageLoader())
                            .start();

                    name.setText(personalDetailBean.getMap().getStore().getStoreName());
                    address.setText(personalDetailBean.getMap().getStore().getAddress());

                    Glide.with(PersonalDetailActivity.this)
                            .load(personalDetailBean.getMap().getCoach().getPhotoUrl())
                            .error(R.mipmap.default_big_bg)
                            .into(photo);
                    itemName.setText(personalDetailBean.getMap().getCoach().getCoachName());
                    DecimalFormat df = new DecimalFormat("######.###");
                    money.setText("￥" + df.format(personalDetailBean.getMap().getCurriculum().getPrice() / 100));

                    RichText.fromHtml(personalDetailBean.getMap().getCoach().getContent() + "").into(richtv);

                    List<String> list = Arrays.asList(personalDetailBean.getMap().getCurriculum().getPhotoUrls().split(","));
                    quickAdapter.clear();
                    quickAdapter.addAll(list);
                    quickAdapter.notifyDataSetChanged();

                    if (personalDetailBean.getMap().getVedio() == null) {
                        videotv.setVisibility(View.GONE);
                        video.setVisibility(View.GONE);
                    } else {
                        video.setUp(personalDetailBean.getMap().getVedio().getVideoUrl()/*"http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4"*/,
                                JZVideoPlayerStandard.SCREEN_WINDOW_LIST,
                                personalDetailBean.getMap().getVedio().getVideoTitle());
                        Glide.with(PersonalDetailActivity.this)
                                .load(personalDetailBean.getMap().getVedio().getPhotoUrl())
                                .error(R.mipmap.default_big_bg)
                                .into(video.thumbImageView);
                    }
                } else {
                    showToast(personalDetailBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                dismissDialog();
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    @OnClick({R.id.location, R.id.buy})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.location:
                Bundle bundle = new Bundle();
                bundle.putString("flag", "personal");
                bundle.putSerializable("storeData", mPersonalDetailBean.getMap().getStore());
                startActivity(MapActivity.class, bundle);
                break;
            case R.id.buy:
                Bundle bundle1 = new Bundle();
                bundle1.putString("flag", "personal");
                bundle1.putSerializable("personal", mPersonalDetailBean.getMap().getCurriculum());
                bundle1.putSerializable("personal_coach", mPersonalDetailBean.getMap().getCoach());
                startActivity(SureOrderActivity.class, bundle1);
                break;
        }
    }
}
